﻿using System;
using System.Net;
using System.Net.Sockets;

namespace CSharp获取本地IPv4
{
    class Program
    {
        public static IPAddress GetLocalIPv4Address()
        {
            IPAddress localIP = null;
            //获取本机所有的IP地址列表
            System.Net.IPAddress[] addressList = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress item in addressList)
            {
                //判断是否是IPv4地址
                if (item.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = item;
                    break;
                }
                else
                    continue;

            }
            return localIP;
        }

        static void Main(string[] args)
        {
            IPAddress iP = GetLocalIPv4Address();
            Console.WriteLine($"ip={iP}");
        }
    }
}
